package com.xxx.date;

import com.xxx.para.ParametersUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    public static final long DAY_YEAR = 365;
    private static final long ONE_MINUTE = 60;
    private static final long ONE_HOUR = 3600;
    private static final long ONE_DAY = 86400;
    private static final long ONE_MONTH = 2592000;
    private static final long ONE_YEAR = 31104000;

    public static Calendar calendar = Calendar.getInstance();

    public static final String defaultFormat = "yyyy-MM-dd HH:mm:ss";

    public static final String ymdFormat = "yyyy-MM-dd";

    public static final String ymChineseFormat = "yyyy年MM月";

    public static final String ymdhmsFormat = "yyyyMMddHHmmss";

    /**
     * 格式化date
     * 默认格式 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static Date nowFormatDate() {
        return nowFormatDate(defaultFormat);
    }

    /**
     * 当前时间
     * @return Date
     */
    public static Date nowDate() {
        return new Date();
    }

    /**
     * 格式化date
     * 默认格式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date,defaultFormat);
    }

    /**
     * 格式化date
     * 默认格式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateYMChinese(Date date) {

        return formatDate(date, ymChineseFormat);
    }

    /**
     * 格式化date
     * 默认格式  format
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前unix时间戳
     * @return
     */
    public static Long nowDateUnix(){
        return (Long) (System.currentTimeMillis() / 1000);
    }

    /**
     * 格式化date
     *
     * @return
     */
    public static Date nowFormatDate(String formatStr) {
        Date retValue = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        try {
            retValue = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retValue;
    }

    /**
     * 当前时间转成字符串 格式为： 2016-01-18 19:25:40
     *
     * @return
     */
    public static String nowTimeToString() {
        Date now = new Date();
        return new SimpleDateFormat(defaultFormat).format(now);
    }

    /**
     * 当前时间转成字符串 格式为： 2016-01-18 19:25:40
     *
     * @return
     */
    public static String nowTimeToString2() {
        Date now = new Date();
        return new SimpleDateFormat("yyyyMMddHHmmss").format(now);
    }

    /**
     * 当前日期转成字符串格式 格式为： 2016-01-18
     *
     * @return
     */
    public static String nowDateToString() {
        Date now = new Date();
        return new SimpleDateFormat("yyyy-MM-dd").format(now);
    }

    public static String dateToString(Date date) {
        if (date == null){
            return "0000-00-00 00:00:00";
        }else {
            return new SimpleDateFormat(defaultFormat).format(date);
        }
    }

    public static String dateToString(Date date, String formatStr) {
        return new SimpleDateFormat(formatStr).format(date);
    }

    public static Date stringToDate(String dateStr, String formatStr) {
        DateFormat df = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    public static Date stringToDateDefault(String dateStr){
        return stringToDate(dateStr, DateUtil.defaultFormat);
    }

    /**
     * "yyyy-MM-dd HH:mm:ss" to Calendar
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Calendar parseStringDateToCalendar(String dateStr) throws Exception {
        if (dateStr == null || dateStr.trim().length() == 0) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date result = df.parse(dateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(result);
        return cal;
    }

    /**
     * 得到几天前的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    
    
    /**
     * 得到几天后的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
//        now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + day);
        return now.getTime();
    }


    /**
     * 得到输入值几小时后的时间
     * @param d
     * @param hour
     * @return
     */
    public static Date getHourAfter(Date d, int hour) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.add(Calendar.HOUR, hour);

        return now.getTime();
    }

    /**
     * 得到输入值几分钟后的时间
     * @param d
     * @param minute
     * @return
     */
    public static Date getMinuteAfter(Date d, int minute) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.add(Calendar.MINUTE, minute);

        return now.getTime();
    }

    /**
     * 得到一个时间戳后的时间
     * 获取10分钟后的时间   seconds= 600 * 1000
     * @param seconds
     * @return
     */
    public static Date getDateAfterSeconds(Integer seconds) {
        seconds *= 1000;
        Date now = new Date();
        Date afterDate = new Date(now.getTime() + seconds);
        return afterDate;
    }

    /**
     * 得到一个时间戳前的时间
     * 获取10分钟后的时间   seconds= 600 * 1000
     * @param seconds
     * @return
     */
    public static Date getDateBeforeSeconds(Integer seconds) {
        seconds *= 1000;
        Date now = new Date();
        Date afterDate = new Date(now.getTime() - seconds);
        return afterDate;
    }

    /**
     * 得到几天后的时间
     *
     * @param day
     * @return
     */
    public static String getDateAfter(int day) {
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return dateToString(now.getTime());
    }

    /**
     * 得到几个月后的时间
     *
     * @param month
     * @return
     */
    public static Date getMonthAfter(int month) {
        Calendar now = Calendar.getInstance();
        Date d = new Date();
        now.setTime(d);
        now.add(Calendar.MONTH, month);

        return now.getTime();
    }

    /**
     * 得到几天后的时间
     *
     * @param day
     * @return
     */
    public static Date getDayAfter(int day){
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间
     *
     * @param day
     * @return
     */
    public static Date getDayAfter(Date date,int day){
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 得到某个事件之后的时间
     *
     * @param d    某个时间
     * @param type 后推的类型 1-年；2-月；5-日
     * @param num  后推的数量 例如 type=1；num=1;表示时间d之后1年的时间
     * @return
     */
    public static Date getDateAfter(Date d, int type, int num) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(type, now.get(type) + num);
        return now.getTime();
    }


    public static Date getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar
                .getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * <p/>
     * 得到起始日期和结束日期之间的天数
     * </P>
     *
     * @param begin 起始日期
     * @param end   结束日期
     * @return 天数
     */
    public static long getdifferentDay(Date begin, Date end) {
        SimpleDateFormat df = new SimpleDateFormat(ymdFormat);
        String first = formatDate(begin, ymdFormat);
        String second = formatDate(end, ymdFormat);
        try {
            begin = df.parse(first);
            end = df.parse(second);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = end.getTime() - begin.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        return day;
    }



    /**
     * <p/>
     * 得到起始日期和结束日期之间的天数
     * </P>
     * 此函数是错误的
     * @param begin 起始日期
     * @param end   结束日期
     * @return 天数
     */
    public static int getDaysInterval(Date begin, Date end) {
        long millisecond = end.getTime() - begin.getTime(); // 日期相减得到日期差X(单位:毫秒)
        return (int) (millisecond / (1000 * 60));
    }

    public static int getDaysAbsolute(Date end, Date begin) {
        Date d1 = DateUtil.stringToDate(
                DateUtil.formatDate(begin, DateUtil.defaultFormat).split(" ")[0],
                "yyyy-MM-dd"
        );

        Date d2 = DateUtil.stringToDate(
                DateUtil.formatDate(end, DateUtil.defaultFormat).split(" ")[0],
                "yyyy-MM-dd"
        );

        return (int)(d2.getTime()-d1.getTime())/(24*60*60*1000);
    }

    /**
     * <p/>
     * 得到起始日期和结束日期之间的天数
     * </P>
     *
     * @param begin 起始日期
     * @param end   结束日期
     * @return 天数
     */
    public static int getDaysInterval(String begin, String end) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return getDaysInterval(df.parse(begin), df.parse(end));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return -1;
    }

    /**
     * <p/>
     * 得到今天起始日期之间的天数
     * </P>
     *
     * @param begin 起始日期
     * @return 天数
     */
    public static long getDaysInterval(String begin) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return getdifferentDay(df.parse(begin), new Date());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return -1;
    }

    /**
     * 到截止时间还要多少天多少小时
     *
     *
     * @return
     */
    public static String restTimeStr(String endTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        String resultStr = "";
        try {
            Date now = new Date();
            Date end = df.parse(endTime);
            long l = end.getTime() - now.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            if (hour == 0 && hour == 0) {
                resultStr = day + "天";
            } else {
                resultStr = day + "天" + hour + "小时";
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultStr;
    }

    /**
     * @author: revelin
     * @Param
     * @Date:  下午3:53 18-5-24
     * @Description: 获取当前时间到0点的秒数
     *
     **/
    public static Integer getDistanceTime(){
        Long current = System.currentTimeMillis();
        Long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        Long twelve=zero+24*60*60*1000;
        Long distance = (twelve - current)/1000;
        return Integer.parseInt(distance + "");
    }

    /**
     * @param format
     * @return yyyy年MM月dd HH:mm MM-dd HH:mm 2012-12-25
     */
    public static String getDate(String format) {
        SimpleDateFormat simple = new SimpleDateFormat(format);
        return simple.format(calendar.getTime());
    }

    /**
     * @return yyyy-MM-dd HH:mm 2012-12-29 23:47
     */
    public static String getDateAndMinute() {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return simple.format(calendar.getTime());
    }

    /**
     * @return yyyy-MM-dd HH:mm:ss 2012-12-29 23:47:36
     */
    public static String getFullDate() {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simple.format(calendar.getTime());
    }

    /**
     * 距离今天多久
     *
     * @param date
     * @return
     */
    public static String fromToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        long time = date.getTime() / 1000;
        long now = System.currentTimeMillis() / 1000;
        long ago = now - time;
        if (ago <= ONE_HOUR){
            if (ago < 300) {
                return "刚刚";
            } else {
                return ago / ONE_MINUTE + "分钟前";
            }
        }else if (ago <= ONE_DAY){
            return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE) + "分钟前";
        }else if (ago <= ONE_YEAR) {
            return (new SimpleDateFormat("MM-dd HH:mm")).format(date);
        } else {
            return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
        }
        // else if (ago <= ONE_DAY * 2)
        // return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
        // + calendar.get(Calendar.MINUTE) + "分";
        // else if (ago <= ONE_DAY * 3)
        // return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
        // + calendar.get(Calendar.MINUTE) + "分";
        // else if (ago <= ONE_MONTH) {
        // long day = ago / ONE_DAY;
        // return day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
        // + calendar.get(Calendar.MINUTE) + "分";
        // } else if (ago <= ONE_YEAR) {
        // long month = ago / ONE_MONTH;
        // long day = ago % ONE_MONTH / ONE_DAY;
        // return month + "个月" + day + "天前"
        // + calendar.get(Calendar.HOUR_OF_DAY) + "点"
        // + calendar.get(Calendar.MINUTE) + "分";
        // } else {
        // long year = ago / ONE_YEAR;
        // int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so
        // month+1
        // return year + "年前" + month + "月" + calendar.get(Calendar.DATE)
        // + "日";
        // }

    }

    /**
     * 距离截止日期还有多长时间
     *
     * @param date
     * @return
     */
    public static String fromDeadline(Date date) {
        long deadline = date.getTime() / 1000;
        long now = (System.currentTimeMillis()) / 1000;
        long remain = deadline - now;
        if (remain <= ONE_HOUR){
            return "只剩下" + remain / ONE_MINUTE + "分钟";
        }else if (remain <= ONE_DAY){
            return "只剩下" + remain / ONE_HOUR + "小时" + (remain % ONE_HOUR / ONE_MINUTE) + "分钟";
        }else {
            long day = remain / ONE_DAY;
            long hour = remain % ONE_DAY / ONE_HOUR;
            long minute = remain % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            return "只剩下" + day + "天" + hour + "小时" + minute + "分钟";
        }

    }

    /**
     * 距离今天的绝对时间
     *
     * @param date
     * @return
     */
    public static String toToday(Date date) {
        long time = date.getTime() / 1000;
        long now = (System.currentTimeMillis()) / 1000;
        long ago = now - time;
        if (ago <= ONE_HOUR){
            return ago / ONE_MINUTE + "分钟";
        }else if (ago <= ONE_DAY){
            return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE) + "分钟";
        }else if (ago <= ONE_DAY * 2){
            return "昨天" + (ago - ONE_DAY) / ONE_HOUR + "点" + (ago - ONE_DAY) % ONE_HOUR / ONE_MINUTE + "分";
        }else if (ago <= ONE_DAY * 3) {
            long hour = ago - ONE_DAY * 2;
            return "前天" + hour / ONE_HOUR + "点" + hour % ONE_HOUR / ONE_MINUTE + "分";
        } else if (ago <= ONE_MONTH) {
            long day = ago / ONE_DAY;
            long hour = ago % ONE_DAY / ONE_HOUR;
            long minute = ago % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            return day + "天前" + hour + "点" + minute + "分";
        } else if (ago <= ONE_YEAR) {
            long month = ago / ONE_MONTH;
            long day = ago % ONE_MONTH / ONE_DAY;
            long hour = ago % ONE_MONTH % ONE_DAY / ONE_HOUR;
            long minute = ago % ONE_MONTH % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            return month + "个月" + day + "天" + hour + "点" + minute + "分前";
        } else {
            long year = ago / ONE_YEAR;
            long month = ago % ONE_YEAR / ONE_MONTH;
            long day = ago % ONE_YEAR % ONE_MONTH / ONE_DAY;
            return year + "年前" + month + "月" + day + "天";
        }

    }

    public static int compare_date(String DATE1, String DATE2) {


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            compare_date(dt1, dt2);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static int compare_date(Date dt1, Date dt2) {
        if (dt1.getTime() > dt2.getTime()) {
            return 1;
        } else if (dt1.getTime() < dt2.getTime()) {
            return -1;
        } else {
            return 0;
        }
    }
    public static int compare_now(Date tDate) {

        Date dt1 = new Date();
        Date dt2 = tDate;

        return compare_date(dt1, dt2);

    }

    public static List<Date> getMonByDate(Date begin) {
        List<Date> result = new ArrayList<>();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(begin);
        do {
            result.add(calendar.getTime());
            calendar.add(Calendar.MONTH, 1);
        }
        while (calendar.getTime().before(new Date()));
        return result;
    }
    
    /**
     * 获取当前时间至当天23:59:59的秒数
     * @author Flying 
     * 2018年1月25日 上午9:54:44
     * @return
     */
    public static int getNowTimeToLastTimeSeconds(){
    	 Calendar calendar = Calendar.getInstance();  
         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),  
                 23, 59, 59);  
         long t1 = calendar.getTimeInMillis();

         long t2 = System.currentTimeMillis();

         return (int)((t1-t2)/1000);
    }
    
    /**
     * 查询当前时间几小时以后的时间(返回的格式:yyyy-MM-dd HH:mm)
     * @author shixx  
     * @date 2018/6/5 14:35
     * @param hours
     * @return
     */
    public static String getHours(String hours) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.HOUR, Integer.parseInt(hours));
        //到账时间
        String arriveTime = sdf.format(calendar.getTime());
        return arriveTime;
    }

    /**
     * 查询当前时间几分钟以后的时间
     * @author shixx
     * @date 2018/6/5 14:35
     * @param hours
     * @return
     */
    public static Date getHours(Integer hours) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MINUTE, hours);
        return calendar.getTime();
    }

    public static Date getHours(Date date,Integer hours) {
        Date now = date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MINUTE, hours);
        return calendar.getTime();
    }

    /**
     * 获取本月第一天
     * @return
     */
    public static Date getAtMonthFirstDay(){
        Calendar cale;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Date date = cale.getTime();
        return date;
    }

    /**
     * 获取本月最后一天
     * @return
     */
    public static Date getAtMonthLastDay(){
        Calendar cale;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        Date date = cale.getTime();
        return date;
    }

    /**
     * 判断当前时间是否在两个时间点中
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean getIsStartDateAndEndDate(Date startDate,Date endDate){
        boolean flag = false;
        long time = new Date().getTime();
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();

        if(time > startTime && time < endTime){
            flag = true;
        }
        return flag;
    }

    public static boolean getIsStartDateAndEndDate(Date startDate,Date endDate,Date checkDate){
        boolean flag = false;
        long time = checkDate.getTime();
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();

        if(time > startTime && time < endTime){
            flag = true;
        }
        return flag;
    }

    /**
     * 获取参数当天的00:00:00和23:59:59
     * @param day
     * @return
     */
    public static List<Date> getStartTimeAndEndTimeDay(Date day){
        List<Date> dateList = new ArrayList<>();

        //day空获取当前时间
        if(ParametersUtil.isNull(day)){
            day = new Date();
        }

        //设施开始时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND,0);
        dateList.add(calendar.getTime());

        //设置结束时间
        calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND,59);
        dateList.add(calendar.getTime());

        return dateList;
    }

    /**
     * 获取参数时间的00:00:00和23:59:59
     * @param startTime
     * @return
     */
    public static List<Date> getStartTimeAndEndTimeDay(Date startTime,Date endTime){
        List<Date> dateList = new ArrayList<>();


        //设施开始时间
        if(ParametersUtil.isNull(startTime)){
            dateList.add(null);
        }else{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND,0);
            dateList.add(calendar.getTime());
        }

        //设置结束时间
        if(ParametersUtil.isNull(endTime)){
            dateList.add(null);
        }else {
            calendar = Calendar.getInstance();
            calendar.setTime(endTime);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            dateList.add(calendar.getTime());
        }

        return dateList;
    }

    /**
     * 获取时间是星期几
     * @param date
     * @return
     */
    public static int getWeekNumber(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return (calendar.get(Calendar.DAY_OF_WEEK) -1);
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     *            开始日期
     * @param endTime
     *            结束日期
     * @return
     */
    public static List<Date> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<Date> days = new ArrayList<>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            // 日期加1(包含结束)
            tempEnd.add(Calendar.DATE, +1);
            while (tempStart.before(tempEnd)) {
                days.add(tempStart.getTime());
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
     * 字符串转换时间
     * @param source 时间字符串
     * @return
     */
    public static  Date  convert(String source) {
        String value = source.trim();
        if ("".equals(value)) {
            return null;
        }
        if(source.matches("^\\d{4}-\\d{1,2}$")){
            return stringToDate(source, "yyyy-MM");
        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
            return stringToDate(source,"yyyy-MM-dd");
        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
            return stringToDate(source, "yyyy-MM-dd HH:mm");
        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
            return stringToDate(source, "yyyy-MM-dd HH:mm:ss");
        }else if(source.matches("^\\d{4}\\d{1,2}\\d{1,2}\\d{1,2}\\d{1,2}\\d{1,2}$")){
            return stringToDate(source, "yyyyMMddHHmmss");
        }else {
            throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
        }
    }



}
